<?php
/**
 *
 * @author Strawberry🍓
 * @date 2021-05-21 4:08 下午
 *
 */

namespace app\common\model\tang;


use app\common\impl\Comm;
use app\common\model\CommonBaseModel;

class BalanceLog extends CommonBaseModel implements Comm
{

    public function getAdminPageData($param = [], $order = 'id desc',$where=[])
    {
//        $model = self::where($where);
        $where[] = ["wm.status","=",1];
        // TODO: Implement getAdminPageData() method.
        if (isset($param['merchant_id']) && $param['merchant_id']){
//            $model->where('wm.merchant_id','=',$param['merchant_id']);
            $where[] = ["wm.merchant_id","=",$param["merchant_id"]];
        }
        if (isset($param['mobile']) && $param['mobile']){
//            $model->where('wm.mobile','like',"%{$param['mobile']}%");
            $where[] = ["wm.mobile","like","%{$param['mobile']}%"];
        }
        if (isset($param['order_sn']) && $param['order_sn']){
//            $model->where('wm.order_sn','like',"%{$param['order_sn']}%");
            $where[] = ["wm.order_sn","like","%{$param["order_sn"]}%"];
        }
        if (isset($param['code']) && $param['code']){
//            $model->where('m.code','=',$param['code']);
            $where[] = ["m.code","=",$param["code"]];
        }

        $data = $this
            ->alias('wm')
            ->leftjoin(['tang_merchant' => 'm'],"wm.merchant_id = m.id")
            ->where($where)
            ->order("wm.create_time desc")
//            ->page(PAGE)
//            ->limit(LIMIT)
            ->field("wm.*,m.merchant_sn,m.name,m.company,m.username,m.mobile,m.code,m.status")
            ->select()
            ->toArray();
        foreach ($data as $k=>$v){
            if(!empty($v['charge_time'])){
                $data[$k]['create_time'] = date('Y-m-d H:i:s',$v['charge_time']);
            }
        }
        $count = count($data);
        //page??总页数
        $page = $param['page'];
        $limit = $param['limit'];
        $datas = array_splice($data, ($page - 1) * $limit, $limit);
        return parent::ajaxResult($datas, $count);

        $count = $model->count("id");
        return parent::ajaxResult($data, $count);
    }

    public function getCreateTimeAttr($value)
    {
        return date('Y-m-d H:i',$value); // TODO: Change the autogenerated stub
    }

    // 退款

    public function Refund($param = [], $order = 'id desc',$where =[]){
        $model = self::where($where);
        if (isset($param['merchant_id']) && $param['merchant_id']){
            $model->where('merchant_id','=',$param['merchant_id']);

        }
        if (isset($param['order_sn']) && $param['order_sn']){
            $model->where('order_sn','like',"%{$param['order_sn']}%");
        }
        if (!empty($param['data'])){
            $data = explode(' - ',$param['data']);
            $model->where('create_time','>=',strtotime($data[0]))->where('create_time','<=',strtotime($data[1]));
        }

        // TODO: Implement getAdminPageData() method.
        $data = $model->with(['merchant','cost'])->order($order)->page(PAGE)->limit(LIMIT)->select()->toArray();
        $count = $model->count("id");

//        if($data){
//            foreach ($data as&$v){
//                if($v['is_admin_create']==0 || $v['is_user']==0){  // 人工创建
//                    $v['fname']  = $v['costType']['cost_name'] ;
//                }else{ // 自主创建
//                    switch ($v['cost_type_id']){
//                        case 1 :
//                            $v['fname'] = '电费' ;
//                            break ;
//                        case 2 :
//                            $v['fname'] = '水费' ;
//                            break ;
//                    }
//                }
//            }
//        }
        return parent::ajaxResult($data, $count);

    }

    public function getAdminPageDatas($param = [], $order = 'id desc',$where=[])
    {
        // TODO: Implement getAdminPageData() method.
        isset($param['id']) && !empty($param['id'])?$where['merchant_id'] =$param['id']:false;
        $data = self::where($where)->with(['merchant'])->order($order)->page(PAGE)->limit(LIMIT)->select()->toArray();
        foreach ($data as $k=>$v){
            if(!empty($v['charge_time'])){
                $data[$k]['create_time'] = date('Y-m-d H:i:s',$v['charge_time']);
            }
        }
        $count = self::where($where)->count("id");
        return parent::ajaxResult($data, $count);
    }

    public function delById($id)
    {
        // TODO: Implement delById() method.
        return parent::del($id);
    }

    public function doAll($data)
    {
        // TODO: Implement doAll() method.
        return parent::doAllData($data);
    }
    public function merchant()
    {
        return $this->hasOne(Merchant::class,'id','merchant_id')->bind([
            'merchant_sn'=>'merchant_sn',
            'code'=>'code',
            'company'=>'company',
        ]);
    }
    public function merchantInfo()
    {
        return $this->hasOne(Merchant::class,'id','merchant_id');
    }
    public function cost()
    {
        return $this->hasOne(Cost::class,'order_sn','order_sn');
    }
}